import gtk
import xmlrpclib
class AdjustmentWindow:
	
	
	def create_window(self):
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect('delete_event', self.delete_event)
		self.box = gtk.VBox(False, 0)
		self.scrollwindow = gtk.ScrolledWindow()
		self.scrollwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		self.scrollwindow.set_size_request(200,200)
		self.listdata = gtk.ListStore(str)
		self.listview = gtk.TreeView(self.listdata)
		self.listcell = gtk.CellRendererText()
		self.listcolumn = gtk.TreeViewColumn('Name', self.listcell, markup=0)
		self.listcolumn.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
		self.listcolumn.set_sort_column_id(0)
		self.listview.append_column(self.listcolumn)
		self.listview.set_search_column(0)
		self.listview.set_reorderable(True)		
		self.createmenu()
		self.box.pack_start(self.menubar, False)
		self.scrollwindow.add(self.listview)
		self.box.pack_start(self.scrollwindow)
		self.window.add(self.box)
		self.window.show_all()

		self.listview.connect_object("row-activated",self.row_activated,None)
		
		
		
	def createmenu(self):
		self.menubar = gtk.MenuBar()
		#file menu
		self.filemenu = gtk.Menu()
		self.openitem = gtk.MenuItem("Open")
		self.quititem = gtk.MenuItem("Quit")
		self.openitem.connect_object("activate", self.openitem_activate, None)
		self.quititem.connect_object ("activate", self.quititem_activate, self.window)
		self.filemenu.append(self.openitem)
		self.filemenu.append(self.quititem)
		self.fileitem = gtk.MenuItem("File")
		self.fileitem.set_submenu(self.filemenu)
		self.menubar.append(self.fileitem)
		
		#Go menu
		self.gomenu = gtk.Menu()
		self.homeitem = gtk.MenuItem("Home")
		self.homeitem.connect_object("activate", self.homeitem_activate, None)
		self.gomenu.append(self.homeitem)
		self.goitem = gtk.MenuItem("Go")
		self.goitem.set_submenu(self.gomenu)
		self.menubar.append(self.goitem)

		

	def openitem_activate(self, widget, data=None):
		pass
		
	def delete_event(self, widget, event, data=None):
		gtk.main_quit()
		return False
		
	def quititem_activate(self, widget, data=None):
		gtk.main_quit()
		return False

	def row_activated(self, widget, row, data=None):
		print "Row Activated ", widget, row
		for value in row:
			print value
		sel = self.listview.get_selection()
		model, iter = sel.get_selected()
		val = model.get_value(iter, 0)
		print sel,model,iter,val
		try:
			returned_list = self.server.FileServer.list(val)
			print returned_list
			self.populate_view(returned_list)
		except:
			print "error occured"

		
	def homeitem_activate(self, widget, data=None):
		if self.register() == True :
			self.populate_view(self.initialList)
			print self.initialList
			
	def populate_view(self,data):
			gtk.ListStore.clear(self.listdata)
			for y,z in data:
				self.listdata.append([y])

	
	def register(self):
		print "In register"
		try:
			self.server_url = 'http://127.0.0.1:8080';
			self.server = xmlrpclib.Server(self.server_url);	
			print self.server
			self.initialList = self.server.FileServer.register()
			return True
		except:
			print "error in server access...check your connection"
		return False
			
	def main(self):
		gtk.main()
	
	def __init__(self):
		self.create_window()
		if self.register() == True :
			self.populate_view(self.initialList)
			print self.initialList
		
		
if __name__ == "__main__":
	window = AdjustmentWindow()
	window.main()
